(function(){
    var gameParams = {
        applySite : 1
    }
    var lottId = "";
    //var addParams = {
    //    name : $(".game-name:eq(0)").val() ,                   //赛事名称
    //    gameid : $(".games:eq(0)").val() ,                    //游戏id
    //    red : $(".red-player:eq(0)").val() ,                  //红方名称
    //    blue : $(".blue-player:eq(0)").val() ,                  //蓝方名称
    //    redImg : "" ,                                           //红方图片
    //    blueImg : "" ,                                          //蓝方图片
    //    sortId : $(".sort:eq(0)").val() ,                      //权重
    //    matchId : $(".match:eq(0)").val() ,                    //赛事id
    //    goldType : $(".gold:eq(0)").val() ,                    //币种
    //    lotteryHandicpJson : ""
    //}
    //获取列表

    var gList = [];
    var mList = [];
    function gamelist(){
        $.ajax({
            url: httpOrigin + "/gameinfo/list.json",
            type:'POST',
            async:true,
            data:gameParams ,
            dataType: "json",
            success: function(data){
                var games = $(".games");
                var html = "";
                if(data.code === 200 ){
                    if( data.hasOwnProperty("t") ) {
                        if( data.t.hasOwnProperty("gameList") ) {
                            gList = data.t.gameList;

                            var id = location.search.match(/\d+/);
                            if( id && id.length ) {
                                getLott( { lotteryId : id[0] } );
                            }
                            for( var i = 0,len = data.t.gameList.length; i < len; i ++) {
                                html += "<option value=\"" + data.t.gameList[i].id + "\">"+ data.t.gameList[i].name +"</option>";
                            }
                            games.html( html );
                        } else {
                            alert("无法获取游戏列表")
                        }
                    } else {
                        alert( data.msg );
                    }
                }
            }
        });
    }
    function matchlist() {
        $.ajax({
            url : httpOrigin + "/matchInfo/list.json" ,
            type:'GET',
            dataType: "json",
            success : function ( data ) {
                if( data.code == 200 ) {
                    if( data.hasOwnProperty("t") ) {
                        if( data.t.hasOwnProperty("matchInfo") ) {
                            var games = $(".match:eq(0)");
                            var html = "";

                            mList = data.t.matchInfo;
                            gamelist();

                            for( var i = 0,len = data.t.matchInfo.length; i < len; i ++) {
                                html += "<option value=\"" + data.t.matchInfo[i].id + "\">"+ data.t.matchInfo[i].matchName +"</option>";
                            }
                            games.html( html );
                        } else {
                            alert("无法获取赛事列表");
                        }
                    } else {
                        alert( data.msg );
                    }
                }
            }
        })
    }
    //新增接口
    function addNewLott( params ) {
        $.ajax({
            url : httpOrigin + "/lottery/add.json" ,
            type : "POST" ,
            data : params ,
            dataType : "json" ,
            success : function ( data ) {
                if( data.code == 200 ) {
                    if( data.hasOwnProperty("t") ) {
                        lottId = data.t;
                        getLott( { lotteryId : lottId } )
                        alert( data.msg );
                    } else {
                        alert("新增失败");
                    }
                } else {
                    alert( data.msg );
                }
            }
        })
    }
    function getLott( params ) {
        var evts = $(".Event");
        $.ajax({
            url : httpOrigin + "/lottery/getLotteryInfoById.json" ,
            type : "POST" ,
            data : params ,
            dataType : "json" ,
            success : function ( data ) {
                if( data.code == 200 ) {
                    if( data.hasOwnProperty("t") && data.t.hasOwnProperty("handicapInfos") ) {
                        lottId = data.t.id;
                        //从修改页面进入
                        if( location.search != "" ) {
                            var item = data.t,idx = 0,idx2 = 0;
                            var imga = new Image(),imgb = new Image();
                            var html = "";

                            for( var i = 0,len = gList.length; i < len; i ++ ) {
                                if( item.gameid == gList[i].id ) {
                                    idx = i;
                                }
                            }
                            for( var i = 0, len = mList.length; i < len; i ++ ) {
                                if( item.matchId == mList[i].id ) {
                                    idx2 = i;
                                }
                            }


                            $(".game-name").val( item.name );
                            $(".sort").val( item.sortId );
                            $(".games").get(0).selectedIndex = idx;
                            $(".match").get(0).selectedIndex = idx2;
                            $(".gold").get(0).selectedIndex = item.goldType == 1 ? 0 : 1;
                            $(".red-player").val( item.red );
                            $(".blue-player").val( item.blue );
                            $("#photoCover1").val( item.redImg );
                            $("#photoCover2").val( item.blueImg )
                            imga.src = item.redImg;
                            $(".imgfile1").html( imga );
                            imgb.src = item.blueImg;
                            $(".imgfile2").html( imgb );


                            if( evts.length > 0 ) {
                                $(".Event").remove();
                            }
                            item.handicapInfos.forEach(function ( val ,key ) {
                                html = $("<div class=\"form-group Event\" data-id=\""+ val.id +"\">" +
                                            "<label>玩法</label>" +
                                            "<input type=\"text\" class=\"form-control w180 play-type\" placeholder=\"玩法\" value=\""+ val.handicapType +"\" />" +
                                                "<label>截至下注</label>" +
                                                "<input type=\"text\" class=\"form-control w180 datepicker\" placeholder=\"截至下注\" value=\""+ new Date(val.quizEndDate).format("yyyy-MM-dd hh:mm:ss") +"\" />" +
                                                "<label>红赔率</label>" +
                                                "<input type=\"text\" class=\"form-control w180 red-odd\" placeholder=\"红赔率\" value=\""+ val.redLotteryObbs +"\" />" +
                                                "<label>蓝赔率</label>" +
                                                "<input type=\"text\" class=\"form-control w180 blue-odd\" placeholder=\"蓝赔率\" value=\""+ val.blueLotteryObbs +"\" />" +
                                                "<br />" +
                                                "<label>竞猜上限</label>" +
                                                "<input type=\"text\" class=\"form-control w180 max-val\" placeholder=\"竞猜上限\" value=\""+ val.maxQuiz +"\" />" +
                                                "<label>竞猜下限</label>" +
                                                "<input type=\"text\" class=\"form-control w180 min-val\" placeholder=\"竞猜下限\" value=\""+ val.minQuiz +"\" />" +
                                            "</div>");

                                html.insertBefore($(".add-btn"));
                            },this);


                            var dates = $(".datepicker");

                            for( var i = 0, len = dates.length; i < len; i ++ ) {
                                dates.eq(i).daterangepicker(
                                    {
                                        autoclose: true,
                                        timePicker24Hour : true ,
                                        timePicker : true ,
                                        todayHighlight:true ,
                                        timePickerSeconds : true  ,
                                        singleDatePicker : true ,
                                        autoApply : false ,
                                        locale : {
                                            format:"YYYY-MM-DD HH:mm:ss",
                                            applyLabel : "确定" ,
                                            cancelLabel : "取消" ,
                                            resetLabel : "清除" ,
                                            daysOfWeek : [ '日', '一', '二', '三', '四', '五', '六' ],
                                            monthNames : [ '一月', '二月', '三月', '四月', '五月', '六月',
                                                '七月', '八月', '九月', '十月', '十一月', '十二月' ],
                                        }
                                    }
                                );
                            }

                        } else {
                            for( var i = 0, len = evts.length; i < len; i ++) {
                                $(evts[i]).attr("data-id" , data.t.handicapInfos[i].id );
                            }
                        }
                    } else {
                        alert("获取赛事失败")
                    }
                } else {
                    alert( data.msg );
                }
            }
        })
    }
    var Page = {
        init:function(){
            this.render();
            this.event();
        },
        //绑定事件
        event:function(){
            //添加赛事
            $(".add").click(function () {
                var html = $("<div class=\"form-group Event\">"                                              +
                                "<label>玩法</label>"                                                        +
                                "<input type=\"text\" class=\"form-control w180 play-type\" placeholder=\"玩法\">" +
                                    "<label>截至下注</label>" +
                                    "<input type=\"text\" class=\"form-control w180 datepicker\" placeholder=\"截至下注\">" +
                                    "<label>红赔率</label>" +
                                    "<input type=\"text\" class=\"form-control w180 red-odd\" placeholder=\"红赔率\">" +
                                    "<label>蓝赔率</label>" +
                                    "<input type=\"text\" class=\"form-control w180 blue-odd\" placeholder=\"蓝赔率\">" +
                                    "<br />" +
                                    "<label>竞猜上限</label>" +
                                    "<input type=\"text\" class=\"form-control w180 max-val\" placeholder=\"竞猜上限\">" +
                                    "<label>竞猜下限</label>" +
                                    "<input type=\"text\" class=\"form-control w180 min-val\" placeholder=\"竞猜下限\">" +
                                "</div>");
                html.insertBefore($(".add-btn"));

                var dates = $(".datepicker");

                for( var i = 0, len = dates.length; i < len; i ++ ) {
                    $(dates).eq(i).daterangepicker(
                        {
                            autoclose: true,
                            timePicker24Hour : true ,
                            timePicker : true ,
                            todayHighlight:true ,
                            timePickerSeconds : true  ,
                            singleDatePicker : true ,
                            autoApply : false ,
                            locale : {
                                format:"YYYY-MM-DD HH:mm:ss",
                                applyLabel : "确定" ,
                                cancelLabel : "取消" ,
                                resetLabel : "清除" ,
                                daysOfWeek : [ '日', '一', '二', '三', '四', '五', '六' ],
                                monthNames : [ '一月', '二月', '三月', '四月', '五月', '六月',
                                    '七月', '八月', '九月', '十月', '十一月', '十二月' ],
                            }
                        }
                    );
                }

            })
            //提交新增
            $(".new-lott").click(function () {
                if( $(".sort").val() == "" ) {
                    alert("请填写权重信息");
                    return;
                }
                if( $(".game-name").val() == "" ) {
                    alert("请填写赛事名称");
                    return;
                }
                if( $(".red-player").val() == "" ) {
                    alert("请填写红方名称");
                    return;
                }
                if( $(".blue-player").val() == "" ) {
                    alert("请填写蓝方名称");
                    return;
                }


                var evts = $(".Event");
                var arr = [];

                for( var i = 0, len = evts.length; i < len; i ++) {
                    var el = $( evts[i] );
                    if( el.find(".play-type").val() == "" ) {
                        alert("请填写第"+ ( i + 1 ) +"个盘口的玩法");
                        return;
                    }
                    if( el.find(".red-odd").val() == "" ) {
                        alert("请填写第"+ ( i + 1 ) +"个盘口的红方赔率");
                        return;
                    }
                    if( el.find(".blue-odd").val() == "" ) {
                        alert("请填写第"+ ( i + 1 ) +"个盘口的蓝方赔率");
                        return;
                    }
                    if( el.find(".max-val").val() == "" ) {
                        alert("请填写第"+ ( i + 1 ) +"个盘口的竞猜上限");
                        return;
                    }
                    if( el.find(".min-val").val() == "" ) {
                        alert("请填写第"+ ( i + 1 ) +"个盘口的竞猜下限");
                        return;
                    }
                    var o = {
                        handicapType : el.find(".play-type").val() ,                                  //玩法
                        endTime : el.find(".datepicker").val() ,                                       //截止日期
                        redLotteryObbs : el.find(".red-odd").val() ,                                  //红方赔率
                        blueLotteryObbs : el.find(".blue-odd").val() ,                               //蓝方赔率
                        maxQuiz : el.find(".max-val").val() ,
                        minQuiz : el.find(".min-val").val() ,
                        id : el.attr("data-id") || 0
                    }
                    arr.push( o );
                }

                var addParams = {
                    name : $(".game-name:eq(0)").val() ,                   //赛事名称
                    gameid : $(".games:eq(0)").val() ,                    //游戏id
                    red : $(".red-player:eq(0)").val() ,                  //红方名称
                    blue : $(".blue-player:eq(0)").val() ,                  //蓝方名称
                    redImg : $("#photoCover1").val() ,                     //红方图片
                    blueImg : $("#photoCover2").val() ,                    //蓝方图片
                    sortId : $(".sort:eq(0)").val() ,                      //权重
                    matchId : $(".match:eq(0)").val() || "20063" ,                    //赛事id
                    goldType : $(".gold:eq(0)").val() ,                    //币种
                    lotteryHandicpJson : "" ,
                    id : lottId || ""
                }
                addParams.lotteryHandicpJson = JSON.stringify( arr );

                addNewLott( addParams );
            })

        },
        //渲染页面
        render:function(){
            matchlist();
        }
    };
    Page.init();
})();